//
//  MyProfit.swift
//  LeGou
//
//  Created by lxm on 2022/8/4.
//

import UIKit

class MyProfit: ZMViewController {
    //MARK: --------------------属性--------------------
    var type = 1
    
    var data = ZMUtil.fastNilBehavior(type: MineModels.ProfitData.self)
    //MARK: --------------------UI控件--------------------
    var seg : XMIndexHeaderView!

    let pro = MyProfitTabel().then { v in
        v.layer.cornerRadius = 12
        v.backgroundColor = .white
    }
    
    let tips = ZMUtil.label(title: "温馨提示：\n1、会员每月需要消费9笔或者完成9个悬赏任务，共富收益和合伙人收益都可变为可用；\n2、未完成任务，次月1号共富收益将清0；", color: "#666666".zm.hex(), font: UIFont.systemFont(ofSize: 14)).then({$0.numberOfLines = 0})
    
//    let tips = ZMUtil.label(title: "温馨提示：\n1、未完成任务，次月1号共富收益将清0；", color: "#666666".zm.hex(), font: UIFont.systemFont(ofSize: 14)).then({$0.numberOfLines = 0})
    
    let taskButton = ZMUtil.button(title: "立即做任务", font: UIFont.systemFont(ofSize: 16), color: .white).then({$0.backgroundColor = Style.mainColor;$0.layer.cornerRadius=23})
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        _ = xmIndexHeaderViewShouldScroll(to: 0, indexView: seg)
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "我的收益"
        view.backgroundColor = "#F5F5F5".zm.hex()
        
        //seg
        let config  = XMIndexHeaerViewConfig()
        config.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        config.fontColor  = UIColor.zm.color999999()
        config.selectFont = UIFont.systemFont(ofSize: 14, weight: .medium)
        config.selectFontColr = Style.mainColor
        config.lineViewCornerRadius = 1.5
        
        seg = XMIndexHeaderView(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: 44), withTitleArray:["今日", "本月", "上月"], animation: false, delegate: self)!
        seg.lineColor = Style.mainColor
        seg.lineWidth = 17
        seg.config = config
        
        view.addSubview(seg)
        
        view.addSubview(pro)
        
        view.addSubview(tips)
        
        view.addSubview(taskButton)
        
        setRightCustomBtn(titleStr: "账单明细", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    }
    
    
    func addConstraints() {
        pro.snp.makeConstraints { make in
            make.top.equalTo(seg.snp.bottom).offset(16)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
        }
        
        tips.snp.makeConstraints { make in
            make.top.equalTo(pro.snp.bottom).offset(16)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
        }
        
        taskButton.snp.makeConstraints { make in
            make.top.equalTo(tips.snp.bottom).offset(48)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.equalTo(46)
        }
        
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        taskButton.rx.tap.subscribe(onNext:{ [unowned self] resp in
            let v = ZMUtil.loadNibViewFullScreen(nibName: DoTaskView.self)
            APPWindow?.addSubview(v)
//            router.push("xm://main/Mine/invite")
        }).disposed(by: disposeBag)
        
        self.data.subscribe(onNext:{ [unowned self] resp in
            let titles = [
               "自购收益",
               "推荐收益",
               "共富收益",
               "合伙人收益",
               "消费奖池分红"
            ]
            if let resp = resp {
                let temp = resp.incomeDataResponseList.sorted(by: {$0.type < $1.type})
                var tData = [[String]]()
                
                var map = [Int : MineModels.ProfitDataList]()
                
                for o in temp {
                    map[o.type] = o
                }
                
                for i in 0 ..< titles.count {
                    let title = titles[i]
                    if let item = map[i + 1] {
                        var flag = "0"
                        if item.type == 3 || item.type == 4 {
                            flag = resp.taskStatus == 1 ? "0" : "1"
                        }
                        tData.append([title, "\(item.quantity)", "¥\(NSDecimalNumber.init(string: item.estimateMoney).stringValue)", "¥\(NSDecimalNumber.init(string: item.settleMoney).stringValue)", flag])
                    } else {
                        tData.append([title, "0", "0", "¥0", "0"])
                    }
                }
                self.pro.data = tData
            }
        }).disposed(by: disposeBag)
        
//        pro.onClick = { [weak self] i in
//            guard let self = self, let type = ProfitIndexType.init(rawValue: i + 1)  else {return}
//            if type == .pool {
//                let vc = ProfitList()
//                vc.type = type
//                vc.title = "消费奖池分红"
//                self.navigationController?.pushViewController(vc, animated: true)
//            } else {
//                let vc = ProfitIndex()
//                vc.type = type
//                self.navigationController?.pushViewController(vc, animated: true)
//            }
//            
//        }
    }
    
    func getInfo() {
        
    }

    override func rightBtnClick(sender: UIButton) {
        router.push("xm://main/Mine/trade_detail", context: ["type" : 1])
    }
    //MARK: --------------------delegate--------------------
}

extension MyProfit : XMIndexHeaderViewDelegate {
    func xmIndexHeaderViewDidselected(with index: Int) {
        
    }
    
    func xmIndexHeaderViewShouldScroll(to index: Int, indexView indexheaderView: XMIndexHeaderView!) -> Bool {
        ZMToast.showActivity()
        MineAPI.myProfitData(["type" : index + 1]).rxRequest(type: MineModels.ProfitData.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            self.data.accept(resp)
            self.type = index + 1
            self.seg.lineViewScrolle(to: index, animation: true)
        }, onError: { _ in

        }).disposed(by: disposeBag)
        return false;
    }
}
